# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from zoom import zoomer
from common.models.user import User, Users
import dzentity, dzpage, dzutil

#------------------------------------------------------------------------
class ListView(dzentity.Entity):
    def __init__(self):
        usage_data = zoomer.db('select user, max(timestamp(date,time)) dt from dz_log group by user')
        usage = dict([(rec[0],dzutil.how_long_ago(rec[1])) for rec in usage_data])

        data = zoomer.db('select * from dz_users order by lastname, firstname')
        self.labels = ['Login','Name','Phone','Email','Registered','Last Seen']
        self.items = [(i.userid,i.loginid,i.firstname+' '+i.lastname,i.phone,'<a href="mailto:%s">%s</a>' % (i.email,i.email),dzutil.how_long_ago(i.dtadd),usage.get(i.loginid,'')) for i in data]

    def get_LIST(self):
        return dzutil.browse(self.items,labels=self.labels,con='user',on_click='show')

    def get_COUNT(self):
        return len(self.items)

    def render(self):
        page = dzpage.Page(dzutil.render('views/users.txt',self))
        page.actions = [dzutil.link_to('Add User',action='add',con='user')]
        return page.render()

class ShowView(dzentity.Entity):
    def __init__(self,user):
        self.user = user

    def render(self):
        page = dzpage.Page(dzutil.render('views/user_show.txt',self.user))
        page.actions = [
            dzutil.link_to('Edit',action='edit',key=self.user.userid),
            dzutil.link_to('Delete',action='confirm_delete',key=self.user.userid),
            dzutil.link_to('Change Password',action='change_password',key=self.user.userid),
        ]
        return page.render()

class DeleteView(dzentity.Entity):
    def __init__(self,user):
        self.user = user

    def render(self):
        page = dzpage.Page(dzutil.render('views/user_delete.txt',form=1,con='user',action='delete',data=self.user))
        return page.render()

class EditView(ShowView):
    def render(self):
        page = dzpage.Page(dzutil.render('views/user_edit.txt',form=1,con='user',action='update',data=self.user))
        page.actions = [dzutil.link_to('List',action='index'),]
        return page.render()

class AddView:
    def __init__(self,user=User()):
        self.user = user

    def render(self):
        page = dzpage.Page()
        page.content = dzutil.render('views/user_edit.txt',form=1,con='user',action='insert',data=self.user)
        return page.render()


class ChangePasswordView:
    def __init__(self,user):
        self.user = user

    def render(self):
        page = dzpage.Page()
        self.user.NEW_PASSWORD = ''
        self.user.CONFIRM = ''
        page.content = dzutil.render('views/user_change_password.txt',form=1,data=self.user,action='update_password')
        return page.render()

